#!/usr/bin/env python
# -*- coding: utf8 -*-
# title       :
# description :
# author      :'ShenMeng'

import os
from functools import partial
from PySide import QtCore, QtGui
from .view_delegate import ViewDelegate


class ListViewWidget(QtGui.QWidget):
    """
    工厂类
    """
    def __init__(self, widget_factory, parent):
        """
        :param widget_factory: Qt Designer-generated widget factory.
        :param parent: Parent widget
        widget must contains:
            button: action button
            box: main frame to set style
            thumbnail: display pixmap label
            label: display info label
        """
        QtGui.QWidget.__init__(self, parent)

        # make sure this widget isn't shown
        self.setVisible(False)
        # set up the UI
        self.ui = widget_factory()
        self.ui.setupUi(self)

        # set up action menu
        self._menu = QtGui.QMenu()
        self._actions = []
        self.ui.button.setMenu(self._menu)
        self.ui.button.setVisible(False)
        self._menu.setStyleSheet("""QMenu{
                                        color:rgb(200,200,200);
                                        font: 9pt \"Arial\";
                                        background:rgb(35,35,35);
                                        border: 1px solid rgb(0, 120, 215);
                                        }
                                    QMenu:item:disabled{
                                        color:rgba(50,50,50);
                                        }
                                    QMenu:item:disabled:selected{
                                        background:rgba(0, 120, 215, 30);
                                        color:rgba(50,50,50);
                                        }
                                    QMenu:item:selected{
                                        background:rgb(0, 120, 215);
                                        }

                                        """)
    def set_actions(self, actions, path):
        """
        添加菜单
        """
        self._actions = actions
        for name in self._actions:
            action = QtGui.QAction(name, self)
            action.triggered.connect(partial(self._actions.get(name), path))
            self._menu.addAction(action)

    def set_button_visible(self, is_visible):
        """
        菜单按钮的显示
        """
        self.ui.button.setVisible(is_visible)

    def set_selected(self, selected):
        """
        设置样式
        """
        if selected:
            self.ui.box.setStyleSheet("""*{background-color: rgba(0,120,210,0);}
                                      #box{border-width: 2px;
                                           border-color: rgb(0,120,210);
                                           border-style: solid;
                                           background-color: rgba(0,120,210,100);
                                           }
                                      #button{border-width: 1px;
                                          border-color: rgb(0,120,210);
                                          border-style: solid;
                                          background-color: rgb(35,35,35);
                                      }""")
        else:
            self.ui.box.setStyleSheet("""*{background-color: rgb(50,50,50);}
                                          #box{border-width: 1px;
                                               border-color: rgb(20,20,20);
                                               border-style: solid;
                                           }""")

    def set_thumbnail(self, pixmap):
        """
        设置预览图片
        """
        self.ui.thumbnail.setPixmap(pixmap)


class ListViewDelegate(ViewDelegate):
    """
    添加菜单项
    """
    def __init__(self, view, action_manager=None):
        """
        Constructor
        :param view: 绘制的窗口
        :param action_manager: 菜单管理器
        """
        self._action_manager = action_manager
        self._view = view
        super(ListViewDelegate, self).__init__(view)

    def _on_before_selection(self, widget, model_index, style_options):
        """
        :param widget: 要绘制的窗口
        :param model_index: 视图中元素的索引
        :param style_options: QT style options
        """
        # do std drawing first
        self._on_before_paint(widget, model_index, style_options)
        item = self._view.model().itemFromIndex(model_index)
        data = item.data(3)
        if data.item_type == 'variant':
            actions = self._action_manager.get(data.asset_step)
            if actions:
                widget.set_actions(actions, data.path)
        else:
            actions = dict()
            actions['location folder'] = os.startfile
            widget.set_actions(actions, data.path)
        return


